<?php
namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\Jumpcategory as AuditcategoryModel;
use app\common\model\Audit as AuditModel;


/**
 * 审核页模块
 */
class Audit extends AdminBase
{
    protected $auditcategory_model;
    protected $audit_model;

    protected function _initialize(){
        parent::_initialize();
        $this->auditcategory_model = new AuditcategoryModel();
        $this->audit_model = new AuditModel();
        $category_level_list = $this->auditcategory_model->getLevelList();
        $this->assign('category_level_list',$category_level_list);
    }

    public function index($page = 1 , $cid = 0 , $keyword = ''){
        $map = [];
        if(empty($keyword)){
            $map['title'] = ['like','%'.$keyword.'%'];
        }

        if($cid > 0){
            $map['wid'] = ['eq',$cid];
        }
        $result = $this->audit_model->alias('w')->field('w.*,think_jumpcategory.name')->where('status',1)->where($map)->join('think_jumpcategory','w.wid = think_jumpcategory.id')->order('w.sort desc')->paginate(15,false,['page'=>$page]);
        $this->assign('audit_list',$result);
        return $this->fetch('index',[
            'keyword'=>$keyword,
            'cid'=>$cid,
        ]);
    }

    public function add(){
        return $this->fetch();
    }

    public function save(){
        if($this->request->isPost()){
            $data = $this->request->param();
            $zip = new \ZipArchive();
            $zip->open(ROOT_PATH.'public'.$data['compress']);
            $wenanname = $zip->getNameIndex(0);//获取目录名称
            $wenan_path = ROOT_PATH.'public/uploads/shenhe/'.$data['wid'];//文案目录
            if (!$zip->extractTo($wenan_path)) {
                $this->error('解压失败,请检查问题');
            }else{
                $zip->close();
                $data['wenan_path'] = '/uploads/shenhe/'.$data['wid'].'/'.$wenanname;
                if($this->audit_model->allowField(true)->save($data)){
                    $this->success('保存成功');
                }else{
                    $this->error('保存失败');
                }
            }
        }
    }   

    public function edit($id){
        $result = $this->audit_model->find($id);
        $this->assign('audit',$result);
        return $this->fetch();
    }

    public function update($id){
        if($this->request->isPost()){
            $data = $this->request->param();
            $zip = new \ZipArchive();
            $zip->open(ROOT_PATH.'public'.$data['compress']);
            $wenanname = $zip->getNameIndex(0);//获取目录名称
            $wenan_path = ROOT_PATH.'public/uploads/wenan/'.$data['wid'];//文案目录
            if (!$zip->extractTo($wenan_path)) {
                $this->error('解压失败,请检查问题');
            }else{
                $zip->close();
                $data['wenan_path'] = '/uploads/wenan/'.$data['wid'].'/'.$wenanname;
                if($this->audit_model->allowField(true)->save($data,$id)){
                    $this->success('修改成功');
                }else{
                    $this->error('修改失败');
                }
            }
        }
    }

    public function delete($id){
        $result = $this->audit_model->where('id',$id)->update(['status'=>0]);
        if($result){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }

    }


    /**
     * 获取下级分类菜单
     */
    public function getChildMenu($id){
        $result = $this->auditcategory_model->field('id,name')->where('pid',$id)->select();
        return json($result);
    }
}